{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、定义一个列表，该列表由2个元组组成，其中元组中元素由(姓名，平时成绩，期末成绩)组成，如列表[('李芳',86,80), (‘张艳',74,75)]，现在要实现为列表添加一个元素('王志’,87,95)的操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('李芳', 86, 80), ('张艳', 74, 75), ('王志', 87, 95)]\n"
     ]
    }
   ],
   "source": [
    "# 定义一个列表\n",
    "students = [('李芳', 86, 80), ('张艳', 74, 75)]\n",
    "\n",
    "# 添加一个元素('王志', 87, 95)\n",
    "students.append(('王志', 87, 95))\n",
    "\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('李芳', 82.4), ('张艳', 74.6), ('王志', 91.80000000000001)]\n"
     ]
    }
   ],
   "source": [
    "# 根据原始列表创建新列表\n",
    "new_students = []\n",
    "\n",
    "for student in students:\n",
    "    name = student[0]\n",
    "    midterm_score = student[1] * 0.4 + student[2] * 0.6\n",
    "    new_students.append((name, midterm_score))\n",
    "\n",
    "print(new_students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('张艳', 74, 75), ('王志', 87, 95)]\n"
     ]
    }
   ],
   "source": [
    "# 删除姓名为'李芳'的元组项\n",
    "for student in students:\n",
    "    if student[0] == '李芳':\n",
    "        students.remove(student)\n",
    "\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'电视机': 3400, '空调': 2300, '洗衣机': 1200}\n"
     ]
    }
   ],
   "source": [
    "# 定义商品信息字典\n",
    "products = {\n",
    "    '电视机': 3400,\n",
    "    '空调': 2300\n",
    "}\n",
    "\n",
    "# 添加新元素\n",
    "products['洗衣机'] = 1200\n",
    "\n",
    "print(products)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "价格最高的商品是：电视机，价格为：3400\n",
      "商品的平均价格为：2300.0\n"
     ]
    }
   ],
   "source": [
    "# 找出字典中价格最高的商品\n",
    "max_price = max(products.values())\n",
    "max_product = [key for key, value in products.items() if value == max_price][0]\n",
    "\n",
    "# 计算商品的平均价格\n",
    "average_price = sum(products.values()) / len(products)\n",
    "\n",
    "print(f\"价格最高的商品是：{max_product}，价格为：{max_price}\")\n",
    "print(f\"商品的平均价格为：{average_price}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'电视机': 3400, '空调': 2800, '洗衣机': 1200}\n"
     ]
    }
   ],
   "source": [
    "# 将空调的价格修改为2800元\n",
    "products['空调'] = 2800\n",
    "\n",
    "print(products)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'空调': 2800, '洗衣机': 1200}\n"
     ]
    }
   ],
   "source": [
    "# 删除电视机商品\n",
    "del products['电视机']\n",
    "\n",
    "print(products)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入购物金额上限：8000\n",
      "请选择操作符：\n",
      "输入商品编号或p打印,m移出,v查看,q退出\n",
      "001\n",
      "商品已加入购物车！\n",
      "请选择操作符：\n",
      "输入商品编号或p打印,m移出,v查看,q退出\n",
      "003\n",
      "商品已加入购物车！\n",
      "请选择操作符：\n",
      "输入商品编号或p打印,m移出,v查看,q退出\n",
      "002\n",
      "商品已加入购物车！\n",
      "购物车商品总价已超过购物金额上限，无法继续购买！\n"
     ]
    }
   ],
   "source": [
    "# 创建商品价格表\n",
    "products = {'001': {'商品名称': '手机', '商品价格': 3000},\n",
    "            '002': {'商品名称': '电脑', '商品价格': 5000},\n",
    "            '003': {'商品名称': '平板', '商品价格': 2000}}\n",
    "\n",
    "# 创建购物车\n",
    "shopping_cart = {}\n",
    "total_price = 0\n",
    "\n",
    "# 输入购物金额上限\n",
    "shopping_limit = int(input(\"请输入购物金额上限：\"))\n",
    "\n",
    "while True:\n",
    "    print(\"请选择操作符：\")\n",
    "    print(\"输入商品编号或p打印,m移出,v查看,q退出\")\n",
    "\n",
    "    user_input = input()\n",
    "\n",
    "    if user_input in products:\n",
    "        # 将商品加入购物车\n",
    "        shopping_cart[user_input] = products[user_input]\n",
    "        total_price += products[user_input]['商品价格']\n",
    "        print(\"商品已加入购物车！\")\n",
    "\n",
    "    elif user_input == 'p':\n",
    "        # 打印购物清单包括商品名称、价格、合计金额和余额\n",
    "        print(\"购物清单：\")\n",
    "        for key, value in shopping_cart.items():\n",
    "            print(\"商品编号：\", key)\n",
    "            for k, v in value.items():\n",
    "                print(k + \":\", v)\n",
    "        print(\"合计金额：\", total_price)\n",
    "        print(\"余额：\", shopping_limit - total_price)\n",
    "\n",
    "    elif user_input == 'm':\n",
    "        # 移出购买商品\n",
    "        print(\"当前购物车清单：\")\n",
    "        for key, value in shopping_cart.items():\n",
    "            print(\"商品编号：\", key)\n",
    "            for k, v in value.items():\n",
    "                print(k + \":\", v)\n",
    "\n",
    "        remove_item = input(\"请输入要移出的商品编号：\")\n",
    "        if remove_item in shopping_cart:\n",
    "            total_price -= shopping_cart[remove_item]['商品价格']\n",
    "            del shopping_cart[remove_item]\n",
    "            print(\"商品已从购物车移除！\")\n",
    "\n",
    "    elif user_input == 'v':\n",
    "        # 查看购物清单\n",
    "        print(\"当前购物车清单：\")\n",
    "        for key, value in shopping_cart.items():\n",
    "            print(\"商品编号：\", key)\n",
    "            for k, v in value.items():\n",
    "                print(k + \":\", v)\n",
    "\n",
    "    elif user_input == 'q':\n",
    "        # 退出程序\n",
    "        break\n",
    "\n",
    "    if total_price > shopping_limit:\n",
    "        print(\"购物车商品总价已超过购物金额上限，无法继续购买！\")\n",
    "        break\n",
    "\n",
    "    if total_price < shopping_limit:\n",
    "        continue\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算结果为: 87733\n"
     ]
    }
   ],
   "source": [
    "def calculate(expression):\n",
    "    def calculate_helper(tokens):\n",
    "        stack = []\n",
    "\n",
    "        num = 0\n",
    "        sign = 1\n",
    "\n",
    "        for token in tokens:\n",
    "            if token.isdigit():\n",
    "                num = num * 10 + int(token)\n",
    "            elif token in ['+', '-']:\n",
    "                stack.append(sign * num)\n",
    "                sign = 1 if token == '+' else -1\n",
    "                num = 0\n",
    "\n",
    "        stack.append(sign * num)\n",
    "\n",
    "        return sum(stack)\n",
    "\n",
    "    tokens = []\n",
    "    num = ''\n",
    "    for char in expression:\n",
    "        if char.isdigit():\n",
    "            num += char\n",
    "        else:\n",
    "            if num:\n",
    "                tokens.append(num)\n",
    "                num = ''\n",
    "            if char in ['+', '-', '*', '/']:\n",
    "                tokens.append(char)\n",
    "\n",
    "    if num:\n",
    "        tokens.append(num)\n",
    "\n",
    "    result = calculate_helper(tokens)\n",
    "\n",
    "    return result\n",
    "\n",
    "# 用户输入表达式\n",
    "expression = \"1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))\"\n",
    "result = calculate(expression)\n",
    "print(\"计算结果为:\", result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
